/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelEcuaciones.java
 *
 * Created on 11-oct-2010, 14:32:33
 */
package interfaz.FuncionPanels.ecuaciones;

import interfaz.NuMethodApp;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import funcion.ManejadorFuncion;
import funcion.Parser;
import java.io.FileOutputStream;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import org.jdesktop.swingx.JXHyperlink;

/**
 *
 * @author frodrig3
 */
public class PanelEcuaciones extends javax.swing.JPanel {

    DefaultTableModel FunctionsModel;
    String[][] availableFunctions;
    String[] tableHeader;
    int amount; //la cantidad de funciones disponibles que hay en el sistema
    Properties Functions;

    /** Creates new form PanelEcuaciones */
    public PanelEcuaciones() {

        Functions = NuMethodApp.getFunctions();
        initComponents();
        initComponents2();
    }

    public void setLinksColor (Color c){
        LinkAddAvailables.setClickedColor(c);
        LinkAddAvailables.setUnclickedColor(c);
        LinkReparse.setClickedColor(c);
        LinkReparse.setUnclickedColor(c);
    }
    /**
     * Metodo interno que carga las funciones disponibles desde Disco
     */
    private void loadAvailableFunctions() {
        try {
            amount = Integer.parseInt(Functions.getProperty("availables")); //hallar numero de funciones disponibles
            int i = 0;
            availableFunctions = new String[amount][1];
            for (i = 0; i < amount; i++) {
                String temp = "function-" + String.valueOf(i + 1);
                availableFunctions[i][0] = Functions.getProperty(temp); //cargar las disponibles de memoria
            }

        } catch (NumberFormatException numberFormatException) {
        } catch (NullPointerException npe) {
        }

    }

    private void getAndShowFunctions() {
        LabelF2PrimaText.setText(NuMethodApp.getD2f());
        LabelFPrimaText.setText(NuMethodApp.getDf());
        LabelFText.setText(NuMethodApp.getF());
        LabelGText.setText(NuMethodApp.getG());
        LabelHText.setText(NuMethodApp.getH());
    }

    private void initComponents2() {
        //
        LinkAddAvailables.setClickedColor(Color.darkGray);
        LinkAddAvailables.setUnclickedColor(Color.darkGray);

        LinkReparse.setClickedColor(Color.darkGray);
        LinkReparse.setUnclickedColor(Color.darkGray);


        showMainControls(false);
        //

        //Obtener los datos de las funciones desde el Frame Principal
        getAndShowFunctions();
        //
        //Usar las propiedades para obtener las funciones disponibles
        loadAvailableFunctions();

        tableHeader = new String[1];
        tableHeader[0] = "Funciones Disponibles";

        TableFunciones.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //Construidos los elementos para la tabla, es hora de cambiar.
        FunctionsModel = new DefaultTableModel(availableFunctions, tableHeader);
        TableFunciones.setModel(FunctionsModel);
        FunctionsModel.fireTableDataChanged();

    }

    // <editor-fold defaultstate="collapsed" desc="Helpful Getters">
    public JXHyperlink getLinkAddAvailables() {
        return LinkAddAvailables;
    }

    public JXHyperlink getLinkReparse() {
        return LinkReparse;
    }

   // </editor-fold>

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        LabelStatus = new javax.swing.JLabel();
        PanelActuales = new javax.swing.JPanel();
        LabelF = new javax.swing.JLabel();
        LabelFPrima = new javax.swing.JLabel();
        LabelF2Prima = new javax.swing.JLabel();
        LabelG = new javax.swing.JLabel();
        LabelH = new javax.swing.JLabel();
        LabelFText = new javax.swing.JLabel();
        LabelFPrimaText = new javax.swing.JLabel();
        LabelF2PrimaText = new javax.swing.JLabel();
        LabelGText = new javax.swing.JLabel();
        LabelHText = new javax.swing.JLabel();
        FieldFunction = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        LinkReparse = new org.jdesktop.swingx.JXHyperlink();
        LinkAddAvailables = new org.jdesktop.swingx.JXHyperlink();
        jPanel3 = new javax.swing.JPanel();
        SPanelDisponibles = new javax.swing.JScrollPane();
        TableFunciones = new org.jdesktop.swingx.JXTable();
        PanelComandos = new javax.swing.JPanel();
        BotonSetF2Prima = new javax.swing.JButton();
        BotonSetFPrima = new javax.swing.JButton();
        BotonSetF = new javax.swing.JButton();
        BotonSetG = new javax.swing.JButton();
        BotonSetH = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        BotonSalvar = new javax.swing.JButton();
        BotonEliminar = new javax.swing.JButton();

        jLabel1.setText("Entra la ecuacion y presiona enter");

        jLabel2.setText("Funciones");

        PanelActuales.setBorder(javax.swing.BorderFactory.createTitledBorder("Funciones Actuales"));
        PanelActuales.setToolTipText("Las funciones que seran usadas por los Metodos de solucion de ecuaciones no lineales");

        LabelF.setText("f(x) =");
        LabelF.setToolTipText("Funcion f(x)");

        LabelFPrima.setText("f'(x) =");

        LabelF2Prima.setText("f''(x) = ");

        LabelG.setText("g(x) =");

        LabelH.setText("h(x) =");

        javax.swing.GroupLayout PanelActualesLayout = new javax.swing.GroupLayout(PanelActuales);
        PanelActuales.setLayout(PanelActualesLayout);
        PanelActualesLayout.setHorizontalGroup(
            PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelActualesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelActualesLayout.createSequentialGroup()
                        .addComponent(LabelH)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LabelHText, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE))
                    .addGroup(PanelActualesLayout.createSequentialGroup()
                        .addGroup(PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(LabelF)
                            .addComponent(LabelFPrima)
                            .addComponent(LabelG)
                            .addComponent(LabelF2Prima))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelActualesLayout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(LabelGText, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE))
                            .addComponent(LabelF2PrimaText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
                            .addComponent(LabelFPrimaText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
                            .addComponent(LabelFText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE))))
                .addContainerGap())
        );
        PanelActualesLayout.setVerticalGroup(
            PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelActualesLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(LabelFText, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE)
                    .addComponent(LabelF))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(LabelFPrima)
                    .addComponent(LabelFPrimaText, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(LabelF2PrimaText, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE)
                    .addComponent(LabelF2Prima))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(LabelGText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(LabelG, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(PanelActualesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelH)
                    .addComponent(LabelHText, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28))
        );

        FieldFunction.setToolTipText("Entra la ecuacion a parsear y presiona Enter");
        FieldFunction.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                FieldFunctionMouseClicked(evt);
            }
        });
        FieldFunction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FieldFunctionActionPerformed(evt);
            }
        });

        LinkReparse.setText("Parsear");

        LinkAddAvailables.setText("Añadir a disponibles");
        LinkAddAvailables.setForeground(new java.awt.Color(102, 153, 255));
        LinkAddAvailables.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LinkAddAvailablesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LinkAddAvailables, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(LinkReparse, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
                .addGap(118, 118, 118))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LinkAddAvailables, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LinkReparse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Ecuaciones Disponibles"));

        TableFunciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        SPanelDisponibles.setViewportView(TableFunciones);

        PanelComandos.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        BotonSetF2Prima.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/f''() icon_032.png"))); // NOI18N
        BotonSetF2Prima.setToolTipText("Configurar funcion seleccionada como f''(x)");
        BotonSetF2Prima.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonSetF2PrimaActionPerformed(evt);
            }
        });

        BotonSetFPrima.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/f'() icon_032.png"))); // NOI18N
        BotonSetFPrima.setToolTipText("Configurar funcion seleccionada como f'(x)");
        BotonSetFPrima.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonSetFPrimaActionPerformed(evt);
            }
        });

        BotonSetF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/f() icon_032.png"))); // NOI18N
        BotonSetF.setToolTipText("Configurar funcion seleccionada como f(X)");
        BotonSetF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonSetFActionPerformed(evt);
            }
        });

        BotonSetG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/g() icon_032.png"))); // NOI18N
        BotonSetG.setToolTipText("Configurar funcion seleccionada como g(x)");
        BotonSetG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonSetGActionPerformed(evt);
            }
        });

        BotonSetH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/h() icon_032.png"))); // NOI18N
        BotonSetH.setToolTipText("Configurar funcion seleccionada como h(x)");
        BotonSetH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonSetHActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Opciones"));

        BotonSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/kappfinder.png"))); // NOI18N
        BotonSalvar.setToolTipText("Salvar las ecuaciones a Disco");
        BotonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonSalvarActionPerformed(evt);
            }
        });

        BotonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/katomic.png"))); // NOI18N
        BotonEliminar.setToolTipText("Elimina la ecuacion seleccionada");
        BotonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(BotonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(BotonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BotonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, Short.MAX_VALUE)
                    .addComponent(BotonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout PanelComandosLayout = new javax.swing.GroupLayout(PanelComandos);
        PanelComandos.setLayout(PanelComandosLayout);
        PanelComandosLayout.setHorizontalGroup(
            PanelComandosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelComandosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelComandosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelComandosLayout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(PanelComandosLayout.createSequentialGroup()
                        .addComponent(BotonSetF)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotonSetFPrima, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotonSetF2Prima)
                        .addGap(6, 6, 6)
                        .addComponent(BotonSetG, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotonSetH, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(197, 197, 197))))
        );

        PanelComandosLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {BotonSetF, BotonSetF2Prima, BotonSetFPrima, BotonSetG, BotonSetH});

        PanelComandosLayout.setVerticalGroup(
            PanelComandosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelComandosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelComandosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BotonSetH)
                    .addGroup(PanelComandosLayout.createSequentialGroup()
                        .addGroup(PanelComandosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(BotonSetG)
                            .addGroup(PanelComandosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(BotonSetFPrima)
                                .addComponent(BotonSetF, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(BotonSetF2Prima))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        PanelComandosLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {BotonSetF, BotonSetF2Prima, BotonSetFPrima, BotonSetG, BotonSetH});

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(PanelComandos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(SPanelDisponibles, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(SPanelDisponibles, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PanelComandos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(FieldFunction, javax.swing.GroupLayout.PREFERRED_SIZE, 602, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(PanelActuales, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(LabelStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(32, 32, 32)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(FieldFunction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LabelStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PanelActuales, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public void addToAvailables (String funcion){
        String[] a = new String[1];
        a[0] = funcion;
        FunctionsModel.addRow(a);
        amount = amount + 1;

        FunctionsModel.fireTableDataChanged();
    }
    private void LinkAddAvailablesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LinkAddAvailablesActionPerformed
        // int rowCount = FunctionsModel.getRowCount();
        addToAvailables(FieldFunction.getText());

        //TODO verificar que el string parseado no se encuente ya en las funciones disponibles



    }//GEN-LAST:event_LinkAddAvailablesActionPerformed

    private void showMainControls(boolean show) {
        if (show) {
            LinkAddAvailables.setVisible(show);
            LinkReparse.setVisible(show);
        } else {
            LinkAddAvailables.setVisible(show);
            LinkReparse.setVisible(show);
        }
    }
    private void FieldFunctionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FieldFunctionActionPerformed
        String function = FieldFunction.getText();
        ManejadorFuncion.getInstance().addFuncion("f", "x", function);
        if (ManejadorFuncion.getInstance().eval("f", 0) != Double.NaN) //Verificar que si se puede parsear la expresion
        {
            showMainControls(true);
            LabelStatus.setText("Expresion parseada correctamente");
            FieldFunction.setForeground(Color.green);
        } else {
            FieldFunction.setForeground(Color.red);
            LabelStatus.setText("Error parseando la expresion");
            showMainControls(false);
        }

    }//GEN-LAST:event_FieldFunctionActionPerformed

    private void FieldFunctionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FieldFunctionMouseClicked
        FieldFunction.setForeground(Color.black);
        LabelStatus.setText("");

        if (LinkAddAvailables.isVisible()) {
            showMainControls(false);
        }

    }//GEN-LAST:event_FieldFunctionMouseClicked

    private void BotonSetFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonSetFActionPerformed
        int row = TableFunciones.getSelectedRow();
        int column = TableFunciones.getSelectedColumn();
        String t = TableFunciones.getStringAt(row, column);

        NuMethodApp.setF(t);
        getAndShowFunctions();

    }//GEN-LAST:event_BotonSetFActionPerformed

    private void BotonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonEliminarActionPerformed
        int row = TableFunciones.getSelectedRow();
        //int column = TableFunciones.getSelectedColumn();

        FunctionsModel.removeRow(row);
        amount = amount - 1;

        FunctionsModel.fireTableDataChanged();
    }//GEN-LAST:event_BotonEliminarActionPerformed

    private void BotonSetFPrimaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonSetFPrimaActionPerformed
        int row = TableFunciones.getSelectedRow();
        int column = TableFunciones.getSelectedColumn();
        String t = TableFunciones.getStringAt(row, column);

        NuMethodApp.setDf(t);
        getAndShowFunctions();
    }//GEN-LAST:event_BotonSetFPrimaActionPerformed

    private void BotonSetF2PrimaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonSetF2PrimaActionPerformed
        int row = TableFunciones.getSelectedRow();
        int column = TableFunciones.getSelectedColumn();
        String t = TableFunciones.getStringAt(row, column);

        NuMethodApp.setD2f(t);
        getAndShowFunctions();
    }//GEN-LAST:event_BotonSetF2PrimaActionPerformed

    private void BotonSetGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonSetGActionPerformed
        int row = TableFunciones.getSelectedRow();
        int column = TableFunciones.getSelectedColumn();
        String t = TableFunciones.getStringAt(row, column);

        NuMethodApp.setG(t);
        getAndShowFunctions();
    }//GEN-LAST:event_BotonSetGActionPerformed

    private void BotonSetHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonSetHActionPerformed
        int row = TableFunciones.getSelectedRow();
        int column = TableFunciones.getSelectedColumn();
        String t = TableFunciones.getStringAt(row, column);

        NuMethodApp.setH(t);
        getAndShowFunctions();
    }//GEN-LAST:event_BotonSetHActionPerformed

    private void BotonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonSalvarActionPerformed
        int t = JOptionPane.showConfirmDialog(this, "Esta operacion guardara las funciones en disco" +
                "\n ¿Estas seguro que deseas continuar?", "Estas seguro", JOptionPane.WARNING_MESSAGE);

        if (t == JOptionPane.OK_OPTION) {

            //Guardar..
            FileOutputStream out = null;
            try {
                //Valores estaticos
                Functions.setProperty("availables", String.valueOf(amount));
                Functions.setProperty("function-F", NuMethodApp.getF());
                Functions.setProperty("function-FPrima", NuMethodApp.getDf());
                Functions.setProperty("function-F2Prima", NuMethodApp.getD2f());
                Functions.setProperty("function-G", NuMethodApp.getG());
                Functions.setProperty("function-H", NuMethodApp.getH());
                //Valores dinamicos
                for (int i = 0; i < amount; i++) {
                    int iHelp = i + 1;
                    String Help = "function-" + String.valueOf(iHelp);
                    String hValue = TableFunciones.getStringAt(i, 0);
                    Functions.setProperty(Help, hValue);
                }
                //Guardar..
                out = new FileOutputStream(".\\Save.nmf");
                Functions.store(out, "Funciones Guardadas");



            } catch (FileNotFoundException ex) {
                Logger.getLogger(PanelEcuaciones.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Imposible guardar las funciones" +
                        "\nNo se encuentra el archivo Save.nmf." +
                        "\n\n por favor construye un archivo Save.nmf e intentalo de nuevo", "Error guardando", JOptionPane.ERROR_MESSAGE);
            } catch (IOException ex) {
                Logger.getLogger(PanelEcuaciones.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(PanelEcuaciones.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_BotonSalvarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonEliminar;
    private javax.swing.JButton BotonSalvar;
    private javax.swing.JButton BotonSetF;
    private javax.swing.JButton BotonSetF2Prima;
    private javax.swing.JButton BotonSetFPrima;
    private javax.swing.JButton BotonSetG;
    private javax.swing.JButton BotonSetH;
    private javax.swing.JTextField FieldFunction;
    private javax.swing.JLabel LabelF;
    private javax.swing.JLabel LabelF2Prima;
    private javax.swing.JLabel LabelF2PrimaText;
    private javax.swing.JLabel LabelFPrima;
    private javax.swing.JLabel LabelFPrimaText;
    private javax.swing.JLabel LabelFText;
    private javax.swing.JLabel LabelG;
    private javax.swing.JLabel LabelGText;
    private javax.swing.JLabel LabelH;
    private javax.swing.JLabel LabelHText;
    private javax.swing.JLabel LabelStatus;
    private org.jdesktop.swingx.JXHyperlink LinkAddAvailables;
    private org.jdesktop.swingx.JXHyperlink LinkReparse;
    private javax.swing.JPanel PanelActuales;
    private javax.swing.JPanel PanelComandos;
    private javax.swing.JScrollPane SPanelDisponibles;
    private org.jdesktop.swingx.JXTable TableFunciones;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration//GEN-END:variables
}
